﻿using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;

using System;
namespace ConfExpert.Forms.Rapor.YeniYapi
{
    public partial class TopEtiketleri : XRapor
    {
        bool NumuneEtiket = false;
        public TopEtiketleri()
        {
            InitializeComponent();
        }

        public TopEtiketleri(bool numuneEtiket)
            : this()
        {
            NumuneEtiket = numuneEtiket;

            if (numuneEtiket)
            {
                xtraTabControl1.ShowTabHeader = DevExpress.Utils.DefaultBoolean.False;
                xtraTabControl1.SelectedTabPage = xtraTabPage2;
            }
        }

        private void TopEtiketleri_Load(object sender, EventArgs e)
        {            
            this.Text = NumuneEtiket?"Numune Etiketleri":"Top Etiketleri";
            beiKopya.Visibility = NumuneEtiket ? DevExpress.XtraBars.BarItemVisibility.Always : DevExpress.XtraBars.BarItemVisibility.Never;

            gv.Init("GetBarcodeHistory2", 18);
            gv.AddSummary("Adet");

            gv.Click += new EventHandler(gv_Click);
            gv.MakeColumnInvisible("BarcodeHistoryId", "Notlar");

            if (NumuneEtiket)
                gv.MakeColumnInvisible("Cikti");
            else
            {
                gv.MakeColumnInvisible("Numune");
                //lblBos.Visible = txtEmptyLabel.Visible = false;
            }
            
            gv.Columns["ModifiedOn"].Visible = true;
            btnPreview.ItemClick += btnPreview_ItemClick;
            btnGuncelle.ItemClick += btnGuncelle_ItemClick;
            gv.MakeReadOnly();
            gv.BestFitColumns();

        }

        void btnGuncelle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gv.EndEditing();
            gv.RefreshDataSource("GetBarcodeHistory2", 18);
            if (NumuneEtiket)
                gv.MakeColumnInvisible("Cikti");
            else
                gv.MakeColumnInvisible("Numune");

            gv.BestFitColumns();
        }

        void btnPreview_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (txtRollId.EditValue.ToStr().Trim() != String.Empty)
            {
                if (txtRollId.EditValue.ToStr().Length < 24)
                    txtInfo.Caption = "R: " + txtRollId.EditValue.ToStr();
                else
                    txtInfo.Caption = string.Empty;

                if (NumuneEtiket)
                {
                    int KopyaSayisi = beiKopya.EditValue.ToInt();
                    if (intParse(txtEmptyLabel.EditValue) > 4)
                    {
                        XtraMessageBox.Show("Bir sayfada 4 sıradan fazla boş etiket olamaz!", "Critical");
                        return;
                    }
                    if (KopyaSayisi == 0)
                    {
                        XtraMessageBox.Show("Kopya sayısı 0 olamaz !", "Critical");
                        return;
                    }
                    NumuneEtiketleri numune = new NumuneEtiketleri(Enums.EtiketTipi.RollId, txtRollId.EditValue.ToStr(), txtEmptyLabel.EditValue.ToInt(-1), KopyaSayisi);
                    reportPresenter2.View(numune);

                    //xtraTabControl1.ShowTabHeader = DevExpress.Utils.DefaultBoolean.False;
                }
                else
                {
                    Du4Barkod barcode1 = new Du4Barkod(Enums.EtiketTipi.RollId, txtRollId.EditValue.ToStr(), txtEmptyLabel.EditValue.ToInt(-1), cbTip1.Checked);
                    reportPresenter1.View(barcode1);
                    xtraTabPage1.PageVisible = barcode1.RowCount > 0;

                    Etiket barcode2 = new Etiket(Enums.EtiketTipi.RollId, txtRollId.EditValue.ToStr(), cbTip1.Checked);
                    reportPresenter2.View(barcode2);
                    xtraTabPage2.PageVisible = barcode2.RowCount > 0;

                    //xtraTabControl1.ShowTabHeader = DevExpress.Utils.DefaultBoolean.True;
                }
            }
        }


        void gv_Click(object sender, EventArgs e)
        {
            GridView view = sender as GridView;

            if (gv.FocusedRowHandle < 0)
                return;

            if (gv.SelectedRowsCount < 1)
                gv.SelectRow(gv.FocusedRowHandle);

            string lst = string.Empty;
            foreach (int rowHandle in gv.GetSelectedRows())
                lst += gv.GetRowCellDisplayText(rowHandle, "GelisPartisiId") + ",";
            lst = lst.Substring(0, lst.Length - 1);
            if (lst.Length < 24)
                txtInfo.Caption = "GP: " + lst;
            else
                txtInfo.Caption = string.Empty;

            /*
        GridHitInfo hi = view.CalcHitInfo(gc.PointToClient(MousePosition));
        if ((hi.InRow || hi.InRowCell) && gv.FocusedRowHandle > -1)
        {
            string liste = gv.GetFocusedRowCellValue("GelisPartisiId").ToString();
            if (liste.Substring(liste.Length - 1) == ",")
                liste = liste.Substring(0, liste.Length - 1);
             */
            if (NumuneEtiket)
            {
                int KopyaSayisi = beiKopya.EditValue.ToInt();
                if (intParse(txtEmptyLabel.EditValue) > 4)
                {
                    XtraMessageBox.Show("Bir sayfada 4 sıradan fazla boş etiket olamaz!", "Critical");
                    return;
                }
                if (KopyaSayisi == 0)
                {
                    XtraMessageBox.Show("Kopya sayısı 0 olamaz !", "Critical");
                    return;
                }
                NumuneEtiketleri numune = new NumuneEtiketleri(Enums.EtiketTipi.GelisPartisiId, lst, txtEmptyLabel.EditValue.ToInt(-1),KopyaSayisi);
                reportPresenter2.View(numune);
            }
            else
            {
                Du4Barkod barcode1 = new Du4Barkod(Enums.EtiketTipi.GelisPartisiId, lst, txtEmptyLabel.EditValue.ToInt(-1), cbTip1.Checked);
                //barcode1.ShowPreview();
                reportPresenter1.View(barcode1);
                xtraTabPage1.PageVisible = barcode1.RowCount > 0;
                //lblBos.Visible = txtEmptyLabel.Visible = barcode1.RowCount > 0;

                Etiket barcode2 = new Etiket(Enums.EtiketTipi.GelisPartisiId, lst, cbTip1.Checked);
                reportPresenter2.View(barcode2);
                xtraTabPage2.PageVisible = barcode2.RowCount > 0;
            }

        }

      


    }
}